<?php

class VideosController extends AppController {
  //public $scaffold;
  public $helpers = array('Html', 'TWSForm', 'Paginator');
  public $components = array('Session','Util');
  public $name = 'Videos';
  public $paginate;

  function __construct($request=null, $response=null) {
    parent::__construct($request, $response);
    $this->paginate = array(
        'limit' => 3,
        'order' => array(
            'Video.modified' => 'desc'
            ));
  }

  function index($companyId) {
    $this->set('title_for_layout','Isle Vow Gallery');
    $this->set("meta_keywords","Videos from Isle Vow, Trinidad and Tobago");
    $this->set("meta_description","A video gallery for Isle Vow.");
    if(!empty($companyId)){
      $this->loadModel('Company');
      $this->Company->id=$companyId;
      $companyInfo = $this->Company->find("first",array("conditions"=>array("Company.id"=>$companyId)));
      if(!empty($companyInfo)){
        $this->set('companyInfo', $companyInfo);
        $videos = $this->paginate('Video', array('Video.company_id' => $companyId));
        if(!empty($videos)){
          for($i=0; $i<sizeof($videos); $i++) {
            $videos[$i]['Video']['youtube_param'] = $this->Util->getUrlQueryStringParameter($videos[$i]['Video']['url'],'v');
          }
        }
        $this->set('videos', $videos);
      } else {
        $this->redirect('/gallery');
      }
    } else {
      $this->redirect('/gallery');
    }

  }

//admin and public version

  function admin_index(){
    $this->layout= 'admin';
    $this->set('videos', $this->paginate('Video'));
  }

  function photographer_index() {
    $this->layout = "photographer";
    $user = $this->Session->read('User');
    $this->set('videos', $this->paginate('Video', array('Video.company_id' => $user['User']['company_id'])));
    $this->render('admin_index');
  }

//admin and public version
  function photographer_add() {
    $this->layout = "photographer";
    $this->loadModel('Company');
    $user = $this->Session->read('User');
    $this->set('options', $this->Company->find('list', array('fields' => array('Company.id', 'Company.name'), 'conditions' => array('Company.id' => $user['User']['company_id']))));
    if ($this->request->is('post')) {
      $this->_processAction($this->request, 'add');
    }
    $this->render("admin_add"); //TODO - Create this view
  }

  function photographer_edit($id=null) {
    $this->layout = "photographer";
    $this->Video->id = $id;
    if ($this->request->is('get')) {
      $this->loadModel('Company');
      $user = $this->Session->read('User');
      $this->set('options', $this->Company->find('list', array('fields' => array('Company.id', 'Company.name'), 'conditions' => array('Company.id' => $user['User']['company_id']))));
      $this->request->data = $this->Video->read();
      $this->render('admin_edit');
    } else {
      //check for delete
      if (isset($this->request->data['deleteBtn'])) {
        $this->photographer_delete($id);
      } else {
        $this->_processAction($this->request, 'edit', $id);
      }
    }
  }

  function photographer_delete($id){
    $this->layout= "photographer";
    $this->delete($id);
  }

    private function delete($id) {
    if ($this->Video->delete($id, true)) {
      $this->Session->setFlash('The record has been deleted.');
      $this->redirect(array('action' => 'index'));
    }
  }

  //admin
  function admin_add() {
    $this->layout = "admin";
    $this->loadModel('Company');
    $this->set('options', $this->Company->find('list', array('fields' => array('Company.id', 'Company.name'), 'order'=>array('Company.name'))));
    if ($this->request->is('post')) {
      $this->_processAction($this->request, 'add');
    }
  }

  //admin
  function admin_edit($id = null) {
    $this->layout = "admin";
    $this->Video->id = $id;
    $this->loadModel('Company');
    $this->set('options', $this->Company->find('list', array('fields' => array('Company.id', 'Company.name'), 'order'=>array('Company.name'))));
    if ($this->request->is('get')) {
      $this->request->data = $this->Video->read();
    } else {
      //check for delete
      if (isset($this->request->data['deleteBtn'])) {
        $this->admin_delete($id);
      } else {
        $this->_processAction($this->request, 'edit', $id);
      }
    }
  }

  private function _processAction($dataIn, $action='add', $id=null) {
      $data = $this->_processRequestData($dataIn);
      $this->Video->set($data);
      if ($this->Video->validates()) {
        if ($this->Video->save($data, false)) {
          $this->Session->setFlash('The record was successfully saved.');
          $this->redirect(array('action' => 'index'));
        } else {
          $this->Session->setFlash('An error occurred and the record was not saved.');
        }
      } else {
        if($action!=='add') {
          $data['Video']['id']=$id;
        }
        $this->request->data['Video'] = $data['Video'];
        $this->set('errors', $this->Video->validationErrors);
      }
      if($action==='add') {
        $this->render('admin_add');
      } else {
        $this->render('admin_edit');
      }
  }
  
  function admin_delete($id) {
    $this->layout= "admin";
    $this->delete($id);
  }

}

?>
